# -*- coding: utf-8 -*- #编码处理
import array
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
project_path = os.path.join(current_dir, '..')
os.chdir(current_dir)   #相对地址处理
sys.path.append(f'{project_path}/common')  #额外的目录依赖
import glog
import ctypes 
import struct
from typing import List, Dict, Any
from enum import Enum


#enum
#!!!!be careful, should not be:
#   xxxxxxx  = 1, 
#   just:
#   XXXXXX =1
class MQTT_CMD(Enum):
    TO_DROP = 0
    HEALTH_STATE_SHAKE = 1
    SHAKE_SENSOR_STATE = 2
    TEMPATURE_SENSOR_STATE = 3
    TEMPATURE_DATA = 4

#两类近似struct的定义，其中一种可以与C代码方便集成
@dataclass
class DumbDataClass:
    topic: str
    payload: bytes  # 或 dict/object

class DumbStructureWith_C_Code(ctypes.Structure):
    _pack_ = 1  # 1 字节对齐，取消填充
    _fields_ = [("ver", ctypes.c_char*2),
                ("sn", ctypes.c_uint32),
                ("strTimeAnchor", ctypes.c_char*20),
                ("u16Array", ctypes.c_uint16*128),
                ("floatArray"), ctypes.c_float*128),

    #探测点....
    def print_rawData_pre8Bytes(self):
        # 读取前8个字节（每个元素2字节，所以前4个元素共8字节）
        first_8_bytes = ctypes.string_at(self.u16Array, 8)

        # 打印这8个字节（以十六进制形式显示）
        glog.info(" ".join([f"{byte:02X}" for byte in first_8_bytes]))

    @staticmethod 
    def get_u16_cptr_of(u16Array):
        u16_values = np.array(u16_values, dtype=np.uint16)  # 强制保留 uint16 类型
        src_ptr = u16_values.ctypes.data_as(ctypes.POINTER(ctypes.c_uint16))
        src_ptr = np.ascontiguousarray(src_ptr)      # 确保连续内存
        return src_ptr
    
    @staticmethod
    def get_float_cptr_of(floatArray):
        u16_values = np.array(u16_values, dtype=np.float32)  # 强制保留 uint16 类型
        src_ptr = u16_values.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
        src_ptr = np.ascontiguousarray(src_ptr)      # 确保连续内存
        return src_ptr

    @staticmethod
    def memcpy(src, tgt, lenOfBytes):
        ctypes.memmove(tgt, src, lenOfBytes)


#普通类
class MainClass:
    def __init__(self, param1):
        self.param1 = param1







